/*
 *
 *  Copyright (C) 2009 GSyC/LibreSoft
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Author : Roberto Calvo Palomino <rocapal@gsyc.es>
 *
 */

package com.LibreGeoSocial.App;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.LibreGeoSocial.Api.GeoNode;
import com.LibreGeoSocial.Api.LibreGeoSocial;
import com.LibreGeoSocial.Api.User;

public class FriendshipList extends ListActivity {
	
	
	FriendShipAdapter mAdapter;
	
	static private ArrayList<GeoNode> mFriendShipList;
	
	static private User dialogUser;
	
	// Dialog
	private static final int DIALOG_ADD_FRIEND = 0;
	
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);		  					
        
		mFriendShipList = LibreGeoSocial.getInstance().getFrienshipInvitations();
		
  		// Adapter for ListView
        mAdapter = new FriendShipAdapter(this);
        setListAdapter(mAdapter);
        

	}
	
	private void updateList()
	{
		mFriendShipList = LibreGeoSocial.getInstance().getFrienshipInvitations();
		mAdapter.notifyDataSetChanged();
		
		if (mFriendShipList.size() == 0)
			this.finish();
	}
	
	 protected void onListItemClick(ListView l, View v, int position, long id) {
		    
		 dialogUser = (User) mFriendShipList.get(position); 
	    	
		 showDialog(DIALOG_ADD_FRIEND);
	    	
	 }

	 protected Dialog onCreateDialog(int id) {
	        
	    	switch (id) {
	        	case DIALOG_ADD_FRIEND:

			    	return new AlertDialog.Builder(this)
			        .setIcon(R.drawable.user_add_48)
			        .setTitle("Add Friend Petition")
			        .setMessage("Do you want to add '" + this.dialogUser.getUsername() +"' as a friend?")
			        .setPositiveButton("Add", new DialogInterface.OnClickListener() {
			            public void onClick(DialogInterface dialog, int whichButton) {
			
			                /* User clicked OK so do some stuff */
			            	if ( LibreGeoSocial.getInstance().setFriend(dialogUser.getId() ) )
			            	{
			            		Toast.makeText(getBaseContext(), 
			     					   "Friend added correctly!", 
			     					   Toast.LENGTH_LONG).show();
			            		
			            		updateList();
			            	}
			            	else
			            	{
			            		Toast.makeText(getBaseContext(), 
				     					   "Error while the friend was added!", 
				     					   Toast.LENGTH_LONG).show();
			            	}
			            }
			        })
			        .setNegativeButton("Deny", new DialogInterface.OnClickListener() {
			            public void onClick(DialogInterface dialog, int whichButton) {
			
			                /* User clicked Cancel so do some stuff */
			                /* User clicked OK so do some stuff */
			            	if ( LibreGeoSocial.getInstance().deleteFriend(dialogUser.getId() ) )
			            	{
			            		Toast.makeText(getBaseContext(), 
			     					   "Friend was ignorated!", 
			     					   Toast.LENGTH_LONG).show();
			            		
			            		updateList();
			            	}
			            	else
			            	{
			            		Toast.makeText(getBaseContext(), 
				     					   "Error while the friend was ignorated!", 
				     					   Toast.LENGTH_LONG).show();
			            	}
			            	
			            }
			        })
			        .setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
			            public void onClick(DialogInterface dialog, int whichButton) {
			
			                /* User clicked Cancel so do some stuff */
			            }
			        })
			        .create();
	    	}
	    	
	    	return null;
	 }
	
	
	public static class FriendShipAdapter extends BaseAdapter {

		private Context mContext;
		
		public FriendShipAdapter (Context c) {
            mContext = c;
        }

		public int getCount() {
			// TODO Auto-generated method stub
			return mFriendShipList.size();
		}

		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			View view;
			
	        if (convertView == null) {
	            // Make up a new view
	            LayoutInflater inflater = (LayoutInflater) mContext
	                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	            view = inflater.inflate(R.layout.friendship_list, null);
	            
	            
	        } else {
	            // Use convertView if it is available
	            view = convertView;
	        }
			
	        
	        User user = (User) mFriendShipList.get(position);
	        
	        ImageView img = (ImageView) view.findViewById(R.id.image);
	        img.setImageResource(R.drawable.user_48);
			
			TextView t = (TextView) view.findViewById(R.id.name);
			t.setText( user.getUsername() );
			
					
	        
			return view;
	        
		}

	}
	

}
